package com.icebergteam.warfacewidget.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;

import android.util.Log;

public class LogManager {
	public static final String className = LogManager.class.getCanonicalName();
	public static final String LOG_FILE = WidgetProvider.ROOT_PATH + "/log.txt";
	public static final String LOG_FLAG = "ALL";//"ALL"; // LOGCAT, LOGFILE, NONE
	public static final String LOG_FILTER = "ALL";//LogManager.getMessage(MainTimer.className, RSSSettings.className);

	public static void LogV(String className, String funcName, Object... arg0) {
		if(!LogManager.LOG_FILTER.equals("ALL")){
			boolean match = false;
			for (String item : LogManager.LOG_FILTER.split(";")) {
				if (item.trim().compareTo(className) == 0) {
					match = true;
					break;
				}
			}
			if (!match) {
				return;
			}
		}
		
		if (LogManager.LOG_FLAG.equals("ALL")
				|| LogManager.LOG_FLAG.equals("LOGCAT")) {
			Log.v(className, LogManager.getLogMessage(funcName, arg0));
		}
		if (LogManager.LOG_FLAG.equals("ALL")
				|| LogManager.LOG_FLAG.equals("LOGFILE")) {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy", Locale.getDefault());
			String currentDateandTime = sdf.format(new Date());
			String str = String.format("%s -- %s -- %s", currentDateandTime,
					className, LogManager.getLogMessage(funcName, arg0));
			LogManager.appendToFile(str);
		}
	}
	
	public static void LogVA(String className, Object... arg0) {
		
		LogManager.LogV(className,LogManager.getExecuteMethodName(4), arg0);
		
	}
	
public static void LogEA(String className, Object... arg0) {
		
		LogManager.LogE(className,LogManager.getExecuteMethodName(4), arg0);
		
	}
	
	public static void LogE(String className, String funcName, Object... arg0) {
		if(!LogManager.LOG_FILTER.equals("ALL")){
			boolean match = false;
			for (String item : LogManager.LOG_FILTER.split(";")) {
				if (item.trim().compareTo(className) == 0) {
					match = true;
					break;
				}
			}
			if (!match) {
				return;
			}
		}
		if (LogManager.LOG_FLAG.equals("ALL")
				|| LogManager.LOG_FLAG.equals("LOGCAT")) {
			Log.e(className, LogManager.getLogMessage(funcName, arg0));
		}
		if (LogManager.LOG_FLAG.equals("ALL")
				|| LogManager.LOG_FLAG.equals("LOGFILE")) {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd.MM.yyyy", Locale.getDefault());
			String currentDateandTime = sdf.format(new Date());
			String str = String.format("%s -- %s -- error: %s", currentDateandTime,
					className, LogManager.getLogMessage(funcName, arg0));
			LogManager.appendToFile(str);
		}
	}

	public static String getLogMessage(String funcName, Object... arg0) {
		String message = String.format("%s - %s", funcName, getMessage(arg0));
		return message;
	}
	private static String getMessage( Object... arg0){
		String message="";
		for (Object string : arg0) {
			message += String.format("%s ; ", string);
		}
		return message;
	}

	public static void appendToFile(String str) {
		try {
			File logFile = new File(LogManager.LOG_FILE);
			if (!logFile.exists()) {
				logFile = ViewUtils.createFile(LogManager.LOG_FILE);
			}
		
			BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
					true));
			buf.append(str);
			buf.newLine();
			buf.newLine();
			buf.close();
		} catch (IOException e) {
			Log.e(className, LogManager.getLogMessage("appendToFile", e.getMessage()));
		}
	}

	public static void removeFile() {
		new File(LogManager.LOG_FILE).delete();
	}
	
	public static String getExecuteMethodName() {
   
       return  LogManager.getExecuteMethodName(4) ;
        
    }
	
	public static String getExecuteMethodName(int depth) {
		if(depth>=0 && depth < Thread.currentThread().getStackTrace().length){
	         return   Thread.currentThread().getStackTrace()[depth].getMethodName();
	    }
		return "";
	}
}
